﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace BiliBiliCSharpStudy.old {
    class MulticastDelegateDemo {
        public static void Demo() {
            //Test1();
            //Test2();
            //Test3();
            //Test4();
            //Test5();
            Test6();
        }

        // 异步
        // 不安全，线程会争抢资源（颜色会错乱）
        public static void Test6() {
            Student11 stu1 = new Student11() { ID = 1, PenColor = ConsoleColor.Yellow };
            Student11 stu2 = new Student11() { ID = 2, PenColor = ConsoleColor.Green };
            Student11 stu3 = new Student11() { ID = 3, PenColor = ConsoleColor.Red };

            Task task1 = new Task(new Action(stu1.DoHomework));
            Task task2 = new Task(new Action(stu2.DoHomework));
            Task task3 = new Task(new Action(stu3.DoHomework));

            task1.Start();
            task2.Start();
            task3.Start();

            for (int i = 0; i < 10; i++) {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Main thread {0}.", i);
                Thread.Sleep(1000);
            }
        }

        // 异步
        // 不安全，线程会争抢资源（颜色会错乱）
        public static void Test5() {
            Student11 stu1 = new Student11() { ID = 1, PenColor = ConsoleColor.Yellow };
            Student11 stu2 = new Student11() { ID = 2, PenColor = ConsoleColor.Green };
            Student11 stu3 = new Student11() { ID = 3, PenColor = ConsoleColor.Red };

            Thread thread1 = new Thread(new ThreadStart(stu1.DoHomework));
            Thread thread2 = new Thread(new ThreadStart(stu2.DoHomework));
            Thread thread3 = new Thread(new ThreadStart(stu3.DoHomework));

            thread1.Start();
            thread2.Start();
            thread3.Start();

            for (int i = 0; i < 10; i++) {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Main thread {0}.", i);
                Thread.Sleep(1000);
            }
        }

        /**
         * 隐式异步调用
         * 不安全，线程会争抢资源（颜色会错乱）
         */
        public static void Test4() {
            Student11 stu1 = new Student11() { ID = 1, PenColor = ConsoleColor.Yellow };
            Student11 stu2 = new Student11() { ID = 2, PenColor = ConsoleColor.Green };
            Student11 stu3 = new Student11() { ID = 3, PenColor = ConsoleColor.Red };

            Action action1 = new Action(stu1.DoHomework);
            Action action2 = new Action(stu2.DoHomework);
            Action action3 = new Action(stu3.DoHomework);

            action1.BeginInvoke(null, null);
            action2.BeginInvoke(null, null);
            action3.BeginInvoke(null, null);

            for (int i = 0; i < 10; i++) {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Main thread {0}.", i);
                Thread.Sleep(1000);
            }
        }

        /**
         * 讲解线程 : 同步调用
         */
        public static void Test3() {
            Student11 stu1 = new Student11() { ID = 1, PenColor = ConsoleColor.Yellow };
            Student11 stu2 = new Student11() { ID = 2, PenColor = ConsoleColor.Green };
            Student11 stu3 = new Student11() { ID = 3, PenColor = ConsoleColor.Red };

            stu1.DoHomework();
            stu2.DoHomework();
            stu3.DoHomework();

            for (int i = 0; i < 10; i++) {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Main thread {0}.", i);
                Thread.Sleep(1000);
            }
        }

        public static void Test2() {
            Student11 stu1 = new Student11() { ID = 1, PenColor = ConsoleColor.Yellow };
            Student11 stu2 = new Student11() { ID = 2, PenColor = ConsoleColor.Green };
            Student11 stu3 = new Student11() { ID = 3, PenColor = ConsoleColor.Red };

            Action action1 = new Action(stu1.DoHomework);
            Action action2 = new Action(stu2.DoHomework);
            Action action3 = new Action(stu3.DoHomework);

            action1 += action2;
            action1 += action3;
            action1.Invoke();
        }

        public static void Test1() {
            Student11 stu1 = new Student11() { ID = 1, PenColor = ConsoleColor.Yellow };
            Student11 stu2 = new Student11() { ID = 2, PenColor = ConsoleColor.Green };
            Student11 stu3 = new Student11() { ID = 3, PenColor = ConsoleColor.Red };

            Action action1 = new Action(stu1.DoHomework);
            Action action2 = new Action(stu2.DoHomework);
            Action action3 = new Action(stu3.DoHomework);

            action1.Invoke();
            action2();
            action3();
        }
    }

    class Student11 {
        public int ID { get; set; }
        public ConsoleColor PenColor { get; set; }

        public void DoHomework() {
            for (int i = 0; i < 5; i++) {
                Console.ForegroundColor = this.PenColor;
                Console.WriteLine("Student {0} doing homework {1} hour(s).", ID, i);
                Thread.Sleep(1000);
            }
        }
    }
}
